<?php
class TBB_Form extends ZendX_JQuery_Form
{
	protected $_datetimePattern = 
	'/^[0-3]{1}[0-9]{1}\-((0[1-9]{1})|(1[0-2]{1}))\-\d{4} ((0[0-9]{1})|(1[0-9]{1})|(2[0-3]{1})):((0[0-9]{1})|([1-5]{1}[0-9]{1})):((0[0-9]{1})|([1-5]{1}[0-9]{1}))$/';
	protected $_timePattern = 
	'/^((0[0-9]{1})|(1[0-9]{1})|(2[0-3]{1})):((0[0-9]{1})|([1-5]{1}[0-9]{1})):((0[0-9]{1})|([1-5]{1}[0-9]{1}))$/';
	protected $_phonePattern = '/^[0-9]{7,20}$/';
		
	protected $_labelInputRowDecorator = array('ViewHelper',
								   			   'Errors', 
								   			   array(array('inputTd' => 'HtmlTag'),
								   		 			 array('tag' => 'td',
								   		 	   		 	   'class' => 'span-1')),
								   		 								    
								  			   array('Label'),
								  
								  			   array(array('labelTd' => 'HtmlTag'),
								  			  		 array('tag' => 'td',
								  			  		       'class' => 'span-1')),
								  		
											   array(array('row' => 'HtmlTag'), 
											   		 array('tag' => 'tr')));
								   		
	protected $_tableDecorator = array('FormElements',
									   array('HtmlTag',
									 	   	 array('tag' => 'table')),
									   array('Fieldset', 
									   		 array('class' => 'fieldsetForm span-8')));	
	protected $_hiddenDecorators = array('ViewHelper');
	protected $_formDecorators = array('FormElements', 'Form');
	protected $_displayGroupDecorators = array('FormElements',
            						 		 array('Fieldset', array('class' => 'fieldsetForm')));	
	protected $_submitDecorators = array('ViewHelper',
										 array('HtmlTag', array('tag' => 'dd')));
										 
	protected function _customizeFormElement($formElement)
	{
		$formElement->setAttribs(array(
			'class' => 'span-6'
		));	
		$formElement->getDecorator('Label')->setOption('class', 'span-4 labelDecorator');
		$formElement->getDecorator('Errors')->setOption('class', 'span-6 errorDecorator');
		return $formElement;	
	}
            						 		 
	///////////////////////////////// validators /////////////////////////////////
	protected function _createStringLengthValidator($minValue, $maxValue, $minMessage, $maxMessage)
	{
		$stringLengthValidator = new Zend_Validate_StringLength(array('min' => $minValue, 'max' => $maxValue));		
		$stringLengthValidator->setMessage($minMessage, Zend_Validate_StringLength::TOO_SHORT);
		$stringLengthValidator->setMessage($maxMessage, Zend_Validate_StringLength::TOO_LONG);
		return $stringLengthValidator;
	}
	
	protected function _createNotEmptyValidator($errorMessage)
	{
		$notEmptyValidator = new Zend_Validate_NotEmpty();
		$notEmptyValidator->setMessage($errorMessage, Zend_Validate_NotEmpty::IS_EMPTY);	
		return $notEmptyValidator;		
	}
	
	protected function _createAlnumValidator($errorMessage)
	{
		$alnumValidator = new Zend_Validate_Alnum();
		$alnumValidator->setMessage($errorMessage, Zend_Validate_Alnum::NOT_ALNUM);
		return $alnumValidator;
	}
	
	protected function _createTimeValidator($errorMessage) 
	{
		$timeValidator = new Zend_Validate_Regex(array('pattern' => $this->_timePattern));
		$timeValidator->setMessage($errorMessage, Zend_Validate_Regex::NOT_MATCH);
		return $timeValidator;
	}
	
	protected function _createDatetimeValidator($errorMessage)
	{
		$datetimeValidator = new Zend_Validate_Regex(array('pattern' => $this->_datetimePattern));
		$datetimeValidator->setMessage($errorMessage, Zend_Validate_Regex::NOT_MATCH);
		return $datetimeValidator;
	}
	
	protected function _createIntValidator($errorMessage)
	{
		$intValidator = new Zend_Validate_Int();
		$intValidator->setMessage($errorMessage, Zend_Validate_Int::NOT_INT);
		return $intValidator;		
	}
	
	protected function _createRegexValidator($pattern, $errorMessage)
	{
		$mobileRegexValidator = new Zend_Validate_Regex(array('pattern' => $pattern));
		$mobileRegexValidator->setMessage($errorMessage, Zend_Validate_Regex::NOT_MATCH);
		return $mobileRegexValidator;	
	}
	
	protected function _createEmailEmailAddressValidator($errorMessage)
	{
		$emailValidator = new Zend_Validate_EmailAddress();
		$emailValidator->setMessage($errorMessage, Zend_Validate_EmailAddress::INVALID_FORMAT);
		return $emailValidator;
	}
}